#include <bits/stdc++.h>

#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define in read()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

int read() {
	int x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

const int N = 1e5 + 10;
const int K = 18;

int rt[N], n, m, c[N], id[N], f[N], dfn[N], tim, dep[N], R[N], fa[K + 1][N], ind[N];
set < int > s[N];
vec G[N];

namespace seg {
	const int N = 1e7 + 10;
	int ch[N][2], val[N], ndn;
	int copy(int x) { int y = ++ndn; ch[y][0] = ch[x][0]; ch[y][1] = ch[x][1]; val[y] = val[x]; return y; }
	void ins(int &x, int pos, int v, int l = 1, int r = n) {
		x = copy(x); val[x] += v; if(l == r) return; int mid = l + r >> 1;
		if(pos <= mid) ins(ch[x][0], pos, v, l, mid); else ins(ch[x][1], pos, v, mid + 1, r);
	}
	int xl, xr;
	int query(int x, int l = 1, int r = n) {
		if(!x) return 0; if(xl <= l && r <= xr) return val[x]; int mid = l + r >> 1;
		int ans = 0;
		if(xl <= mid) ans += query(ch[x][0], l, mid); if(xr > mid) ans += query(ch[x][1], mid + 1, r);
		return ans;
	}
	int tquery(int x, int l, int r) { xl = l; xr = r; return query(x); }
}

void dfs(int x, int p) {
	dep[x] = dep[p] + 1; ind[dfn[x] = ++tim] = x; fa[0][x] = p; rep(i, 1, K) fa[i][x] = fa[i - 1][fa[i - 1][x]];
	for(auto y : G[x]) dfs(y, x);
	R[x] = tim;
}

int lca(int x, int y) {
	if(dep[x] < dep[y]) swap(x, y);
	per(i, K, 0) if(dep[fa[i][x]] >= dep[y]) x = fa[i][x];
	if(x == y) return x;
	per(i, K, 0) if(fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y];
	return fa[0][x];
}

void solve() {
	n = in, m = in, tim = 0, seg :: ndn = 0; 
	rep(i, 1, n) G[i].clear(), rt[i] = 0;
	rep(i, 1, n) c[i] = in, id[i] = i, s[i].clear();
	rep(i, 2, n) f[i] = in, G[f[i]].eb(i);
	dfs(1, 0);
	sort(id + 1, id + n + 1, [](int a, int b) { return dep[a] < dep[b]; });
	rep(i, 1, n) {
		int x = id[i]; if(dep[x] != dep[id[i - 1]]) rt[dep[x]] = rt[dep[x] - 1];
		s[c[x]].insert(dfn[x]);
		auto it = s[c[x]].find(dfn[x]); 
		int pre = ind[it == s[c[x]].begin() ? 0 : *prev(it)];
		int nxt = ind[it == --s[c[x]].end() ? 0 :  *next(it)];
		seg :: ins(rt[dep[x]], dfn[x], 1); if(pre) seg :: ins(rt[dep[x]], dfn[lca(x, pre)], -1);
		if(nxt) seg :: ins(rt[dep[x]], dfn[lca(x, nxt)], -1);
		if(pre && nxt) seg :: ins(rt[dep[x]], dfn[lca(pre, nxt)], 1);
	} rep(i, dep[id[n]] + 1, n) rt[i] = rt[i - 1];
	int ans = 0;
	while(m--) {
		int x = in ^ ans, d = in ^ ans;
		ans = seg :: tquery(rt[min(dep[x] + d, n)], dfn[x], R[x]);
		printf("%d\n", ans);
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	int T = in; while(T--) solve();
	return 0;
}
